home *** CD-ROM | disk | FTP | other *** search
/ L' Effet Pommier 3 / L'Effet Pommier - Volume 03.iso / Programmation / Alpha ƒ / Tcl / SystemCode / filesets.tcl < prev    next >
Text File  |  1996-01-07  |  10KB  |  341 lines

  1. #===============================================================================================
  2. # Alpha calls two fileset-related routines, 'getCurrFileSet', and 
  3. # 'getFileSetNames'. Alpha will also attempt to set the variable 'currFileSet'
  4. # on occasion, but this isn't critical.
  5. #===============================================================================================
  6.  
  7. #===========================================================================
  8. # The filesets.
  9. #===========================================================================
  10.  
  11. # Build some filesets on the fly.
  12. catch {unset fileSets}
  13. catch {unset currFileSet}
  14. set gfileSets(Help) "$HOME:Help:*"
  15. set gfileSets(System) "$HOME:Tcl:SystemCode:*.tcl"
  16. set gfileSets(User) "$HOME:Tcl:UserCode:*.tcl"
  17.  
  18. # Default curr fileset is the first one. Can be changed in 'userStartup.tcl'.
  19. set currFileSet [lindex [array names gfileSets] 0]
  20.  
  21. #===========================================================================
  22. # The support routines.
  23. #===========================================================================
  24. # Called from Alpha to get list of files for current file set.
  25. proc getCurrFileSet {} {
  26.     global fileSets
  27.     global currFileSet
  28.     return $fileSets($currFileSet)
  29. }
  30. # Called from Alpha to get names. The first name returned is taken to 
  31. # be the current fileset.
  32. proc getFileSetNames {} {
  33.     global fileSets
  34.     global currFileSet
  35.     set ind [lsearch [array names fileSets] $currFileSet]
  36.     if {$ind < 0} {set ind 0}
  37.     return [linsert [lsort [lreplace [array names fileSets] $ind $ind]] 0 $currFileSet]
  38. }
  39.  
  40.  
  41. # Keep 'sets' menu up to date.
  42. trace vdelete currFileSet w shadowCurrFileSet
  43. trace variable currFileSet w shadowCurrFileSet
  44. proc shadowCurrFileSet {nm1 nm2 op} {
  45.     global fileSets
  46.     global currFileSet
  47.     foreach name [array names fileSets] {
  48.         if {$name == $currFileSet} {
  49.             markMenuItem -m choose $name on
  50.         } else {
  51.             markMenuItem -m choose $name off
  52.         }
  53.     }
  54.     return $currFileSet
  55. }
  56.  
  57. # Called in response to user changing filesets from the fileset menu.
  58. proc changeFileSet {menu item} {
  59.     global currFileSet gfileSets tagFile
  60.     
  61.     markMenuItem -m choose $currFileSet off
  62.     set currFileSet $item
  63.     markMenuItem -m choose $currFileSet on
  64.  
  65. # Bring in the tags file for this fileset
  66.     set dir [file dirname $gfileSets($item)]
  67.     set fname "$dir:cTags"
  68.     if {[file exists $fname]} {
  69.         if {[askyesno "Use tag file from folder \"$dir\" ?"] == "yes"} {
  70.             set tagFile $fname
  71.         }
  72.     }
  73. }
  74.  
  75.  
  76.  
  77. #===========================================================================
  78. # Add fileset.
  79. #===========================================================================
  80. proc createFileset {} {
  81.     global gfileSets fileSets currFileSet
  82.     
  83.     set name [getline "New fileset name:" ""]
  84.     if {![string length $name]} return
  85.     
  86.     set dir [string trim [get_directory -p "New fileset dir:"] ":"]
  87.     if {![string length $dir]} return
  88.     
  89.     set filePat [getline "File pattern:" "*"]
  90.     if {![string length $filePat]} return
  91.     
  92.     set "gfileSets($name)" "$dir:$filePat"
  93.     menu -n choose -m -p changeFileSet [lsort [array names fileSets]]
  94.     set currFileSet $name
  95.  
  96.     if {[askyesno "Save new fileset?"] == "yes"} {
  97.         addArrDef gfileSets $name "$dir:$filePat"
  98.     }
  99.     rebuildFilesetMenu
  100.     return $name
  101. }
  102.  
  103.  
  104. # Open entire fileset.
  105. proc openEntireFileset {} {
  106.     global fileSets
  107.     set name [eval [list prompt "Open which fileset?" [lindex [array names fileSets] 0] "FileSet:"] [lsort -ignore [array names fileSets]]]
  108.     foreach f $fileSets($name) {
  109.         edit $f
  110.     }
  111. }
  112.  
  113. # Create a fileset fromt containing all windows currently open.
  114. proc createFilesetFromWins {} {
  115.     global fileSets currFileSet
  116.     
  117.     set name [prompt "Create fileset containing current windows under what name?" "OpenWins"]
  118.     set currFileSet $name
  119.     addArrDef fileSets $name [winNames -f]
  120.     set fileSets($name) [winNames -f]
  121.     rebuildFilesetMenu
  122. }
  123.  
  124.         
  125.  
  126. #===========================================================================
  127. # Dump fileset to current window. If you dump at the end of this file,
  128. # the fileset will be reloaded the next time you run Alpha.
  129. #===========================================================================
  130. proc dumpFileset {} {
  131.     global fileSets
  132.     global currFileSet
  133.     if {![catch {prompt "Fileset name:" $currFileSet} name]} {
  134.         insertText "set \"fileSets($name)\" \{\r"
  135.         foreach file "$fileSets($name)" {
  136.             insertText "\t\"$file\"\r"
  137.         }
  138.         insertText "\}\r"
  139.     }
  140. }
  141.  
  142.  
  143.  
  144. #================================================================================
  145. # Edit a file from a fileset via list dialogs (no mousing around).
  146. #================================================================================
  147. proc editFile {} {
  148.     global fileSets
  149.     
  150.     set fset [listpick -p {Fileset?} [lsort -ignore [concat {*recent*} [array names fileSets]]]]
  151.     if {[string length $fset]} {
  152.         if {$fset == {*recent*}} {return [editRecentFile]}
  153.         foreach f $fileSets($fset) {
  154.             lappend disp [file tail $f]
  155.         }
  156.         foreach res [listpick -l -p {File?} [lsort -ignore $disp]]  {
  157.             set ind [lsearch $fileSets($fset) \*:$res]
  158.             catch {edit [lindex $fileSets($fset) $ind]}
  159.         }
  160.     }
  161. }
  162.  
  163.  
  164. proc browseFileset {{fset ""}} {
  165.     global fileSets tileLeft tileTop tileWidth errorHeight
  166.  
  167.     if {![string length $fset]} {
  168.         set fset [listpick -p {Fileset?} [lsort -ignore [concat {*recent*} [array names fileSets]]]]
  169.     }
  170.  
  171.     foreach f $fileSets($fset) {
  172.         append text "\t[file tail $f]\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t░$f\r"
  173.     }
  174.     new -n "* FileSet '$fset' Browser *" -g $tileLeft $tileTop 200 $errorHeight
  175.     global winModes
  176.     set name [lindex [winNames] 0]
  177.     changeMode [set winModes($name) Brws]
  178.  
  179.     insertText "(<cr> to go to message)\r-----\r$text\r"
  180.     goto 0
  181.     select [nextLineStart [nextLineStart 0]] [nextLineStart [nextLineStart [nextLineStart 0]]]
  182.     setWinInfo dirty 0
  183.     setWinInfo read-only 1
  184.     message ""
  185. }    
  186.  
  187.  
  188. proc deleteFileset {} {
  189.     global fileSets gfileSets currFileSet
  190.     
  191.     set fset [listpick -p {Fileset?} [lsort -ignore [array names fileSets]]]
  192.      if {$currFileSet == $fset} {catch {set currFileSet System}}
  193.  
  194.     if {[askyesno "Delete fileset \"$fset\"?"] == "yes"} {
  195.         catch {unset "fileSets($fset)"}
  196.         catch {unset "gfileSets($fset)"}
  197.         
  198.         if {[askyesno "Permanently?"] == "yes"} {
  199.             removeArrDef gfileSets $fset
  200.         }
  201.         rebuildFilesetMenu
  202.     }
  203. }
  204.  
  205.  
  206. #===============================================================================
  207. proc replaceInFileset {} {
  208.     global fileSets
  209.     
  210.     set from [prompt "Search string:" ""]
  211.     set to [prompt "Replace string:" ""]
  212.     set fset [listpick -p "Which fileset?" [array names fileSets]]
  213.  
  214.     if {[buttonAlert "Save all windows?" "Yes" "Cancel"] != "Yes"} return
  215.     saveAll
  216.     
  217.     set cid [scancontext create]
  218.     scanmatch $cid $from {
  219.         set matches($f) 1
  220.     }
  221.     foreach f $fileSets($fset) {
  222.         if {![catch {set fid [open $f]}]} {
  223.             message "Looking at '[file tail $f]'╔"
  224.             scanfile $cid $fid
  225.             close $fid
  226.         }
  227.     }
  228.     scancontext delete $cid
  229.  
  230.     foreach f [array names matches] {
  231.         message "Modifying $f╔"
  232.         set cid [open $f "r"]
  233.         if {[regsub -all $from [read $cid] $to out]} {
  234.             set ocid [open $f "w+"]
  235.             puts -nonewline $ocid $out
  236.             close $ocid
  237.         }
  238.         close $cid
  239.     }
  240.     
  241.     if {[buttonAlert "Revert affected windows?" "Yes" "No"] == "Yes"} {
  242.         foreach f [array names matches] {
  243.             bringToFront $f
  244.             revert
  245.         }
  246.     }
  247.     message ""
  248. }
  249.  
  250. #================================================================================
  251. # Create a heirarchical fileset menu that allows you 
  252. # to open any file in any fileset.
  253. #
  254. # Doesn't bother trying to specialcase names or pathnames that have
  255. # non-alphanumeric characters in them.
  256.  
  257. proc filesetProc {menu item} {
  258.     global fileSets HOME
  259.     if {$item == "Edit File"} {
  260.         editFile
  261.         return
  262.     } elseif {$item == "Create"} {
  263.         createFileset
  264.         return
  265.     } elseif {$item == "Delete"} {
  266.         deleteFileset
  267.         return
  268.     } elseif {$item == "Help"} {
  269.         editMark "$HOME:Help:Manual" "File Sets" -r
  270.         return
  271.     }
  272.     if {[set match [lsearch $fileSets($menu) *:$item]] >= 0} {
  273.         edit [lindex $fileSets($menu) $match]
  274.     }
  275. }    
  276.  
  277.  
  278. proc rebuildFilesetMenu {} {
  279.     global fileSets gfileSets fsetMenuName currFileSet alphaLite
  280.  
  281.     if {$alphaLite} {return [rebuildFilesetMenuLite]}
  282.     
  283.     foreach f [array names gfileSets] {
  284.         catch {set "fileSets($f)" [glob -t TEXT "$gfileSets($f)"]}
  285.     }
  286.     
  287.     foreach f [lsort [array names fileSets]] {
  288.         if {$f == "Help"} continue
  289.         set menu {}
  290.         foreach m "$fileSets($f)" {
  291.             lappend menu "[file tail $m]\&"
  292.         }
  293.         lappend sets [list menu -s -m -n $f -p filesetProc [lsort -i $menu]]
  294.     }
  295.     menu -m -n $fsetMenuName -p filesetProc [concat {{/'Edit File} {menu -n Utilities {}} "Help" "(-"} $sets]
  296.     
  297.     menu -n "Utilities" {
  298.         {menu -n choose -p changeFileSet {}}
  299.         "createFileset"
  300.         "deleteFileset"
  301.         "(-"
  302.         "createThinkFileset"
  303.         "createWarriorFileset"
  304.         "(-"
  305.         "createFilesetFromWins"
  306.         "openEntireFileset"
  307.         "(-"
  308.         "/T<I<OfindTag"
  309.         "createTagFile"
  310.         "(-"
  311.         "replaceInFileset"
  312.         "(-"
  313.         "rebuildFilesetMenu"
  314.         }
  315.  
  316.     menu -n choose -m -p changeFileSet [lsort [array names fileSets]]
  317.     markMenuItem -m choose $currFileSet on
  318. }
  319.  
  320.  
  321. proc rebuildFilesetMenuLite {} {
  322.     global fileSets gfileSets 
  323.  
  324.     foreach f [array names gfileSets] {
  325.         catch {set "fileSets($f)" [glob -t TEXT "$gfileSets($f)"]}
  326.     }
  327.     
  328.     foreach f [lsort [array names fileSets]] {
  329.         if {$f == "Help"} continue
  330.         set menu {}
  331.         foreach m "$fileSets($f)" {
  332.             lappend menu "[file tail $m]\&"
  333.         }
  334.         lappend sets [list menu -s -m -n $f -p filesetProc [lsort -i $menu]]
  335.     }
  336.     menu -m -n filesets -p filesetProc [concat {{/'Edit File} Create Delete "(-" {menu -m -n Recent {}}} $sets]
  337. }
  338.  
  339. if {!$alphaLite} {insertMenu $fsetMenuName}
  340.  
  341.